const cheerio = require('cheerio')
const http = require('http')
const fs = require('fs')
const url = require('url')
const download = require('./utils/download')
const httpUtil = require('./utils/httpUtil')

//todo 下载百度图片

getBaidu('阿曼达·克鲁')



function geturl(u) {
    let u1 = url.parse(u)
    console.log(u1)
}

function getBaidu(q) {
    q = encodeURI(q)
    let i = 1;
    let url = `https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&fm=detail&lm=-1&st=-1&sf=2&fmq=1496604255318_D_R&fm=detail&pv=&ic=0&nc=1&z=&se=&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&hs=2&word=${q}`
    httpUtil.getStatusCode(url)
    httpUtil.getUrl(url)
        .then(function(buf) {
            fs.writeFile('/tmp/baidu.html', buf, err => { console.log(err) })

            // let $ = cheerio.load(buf.toString('utf-8'))
            // let detailurl = $('.people-img>a').attr('href')


            // httpUtil.getUrl(detailurl).the(function(buf2) {
            //     let $ = cheerio.load(buf2.toString('utf-8'))
            //     var downloadurl = $('.bar-btn.btn-download').attr('href')
            //     download.downloadUrl(downloadurl, 'download', i)
            //     i++;
            // })
        })
        .catch(function(err) {
            console.log(err)
        })
}